﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AvatarHelper;
using Avatar.Model;

public partial class Admin_Category : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            btn_Add.Visible = true;
            panel_Add.Visible = false;
            panel_Edit.Visible = false;
        }
    }
    protected void btn_UpdateSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            if (string.IsNullOrEmpty(txt_NameUpdate.Text)) throw new Exception("职位名称不能为空");
            
            using (var _adc = DBHelper.GetDataContext())
            {
                Button _b = sender as Button;
                int _id = Convert.ToInt32(_b.CommandArgument);

                var _position=_adc.Position.Where(p=>p.ID==_id).FirstOrDefault();
                if(_position==null)throw new Exception("职位不存在");

                _position.Name = txt_NameUpdate.Text;

                _adc.SubmitChanges();

                btn_Add.Visible = true;
                panel_Add.Visible = false;
                panel_Edit.Visible = false;
                GridView1.DataBind();
            }
        }
        catch (Exception _e)
        {
            lbl_Error.Text = "编辑职位名称出错:"+_e.Message;
        }
    }
    protected void btn_UpdateCancel_Click(object sender, EventArgs e)
    {
        btn_Add.Visible = true;
        panel_Add.Visible = false;
        panel_Edit.Visible = false;
        GridView1.DataBind();
    }
    protected void btn_AddSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            if (string.IsNullOrEmpty(txt_NameAdd.Text)) throw new Exception("职位名称不能为空");

            using (var _adc = DBHelper.GetDataContext())
            {

                var _position = _adc.Position.Where(p => p.Name == txt_NameAdd.Text).FirstOrDefault();
                if (_position != null) throw new Exception("已存在相同名称职位");
                _position = new Position();

                _position.Name = txt_NameAdd.Text;
                _position.FatherID = Convert.ToInt32(DropDownList1.SelectedValue);

                _adc.Position.InsertOnSubmit(_position);
                _adc.SubmitChanges();

                txt_NameAdd.Text = "";

                btn_Add.Visible = true;
                panel_Add.Visible = false;
                panel_Edit.Visible = false;
                GridView1.DataBind();
            }
        }
        catch (Exception _e)
        {
            lbl_Error.Text = "*" + _e.Message;
        }
    }
    protected void btn_Update_Click(object sender, EventArgs e)
    {
        using (var _adc = DBHelper.GetDataContext())
        {

            Button _b = sender as Button;
            int _id = Convert.ToInt32(_b.CommandArgument);
            var _d = _adc.Position.Where(p => p.ID == _id).FirstOrDefault();
            if (_d != null)
            {
                txt_NameUpdate.Text = _d.Name;
                btn_UpdateSubmit.CommandArgument = _d.ID.ToString();

                btn_Add.Visible = false;
                panel_Add.Visible = false;
                panel_Edit.Visible = true;
            }
        }
    }
    protected void btn_Delete_Click(object sender, EventArgs e)
    {
        panel_Edit.Visible = false;
        try
        {
            using (var _adc = DBHelper.GetDataContext())
            {
                Button _b = sender as Button;
                int _id = Convert.ToInt32(_b.CommandArgument);

                var _position = _adc.Position.Where(p => p.ID == _id).FirstOrDefault();

                if (_position == null) throw new Exception("职位不存在");
                if (_position.Job.Count > 0) throw new Exception("此职位下有职位信息若干，不能删除。");

                _adc.Position.DeleteOnSubmit(_position);
                _adc.SubmitChanges();

                btn_Add.Visible = true;
                panel_Add.Visible = false;
                panel_Edit.Visible = false;
                GridView1.DataBind();
            }
        }
        catch (Exception _e)
        {
            lbl_Error.Text = "删除失败:" + _e.Message;
        }
    }
    protected void btn_Add_Click(object sender, EventArgs e)
    {
        btn_Add.Visible = false;
        panel_Add.Visible = true;
        panel_Edit.Visible = false;
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        btn_Add.Visible = true;
        panel_Add.Visible = false;
        panel_Edit.Visible = false;
    }
}
